<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class Login extends CI_Controller{
    private $b_Check = false;
    function __construct() {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->library(array('form_validation','session'));

        #Táº£i model
        $this->load->model(array('login_model'));
    }
    
   /* public function index($msg = NULL){
        $data['msg'] = $msg;
        $this->load->view("back_end/login_view",$data);
    }
    
    public function process(){
        $this->load->model(Login_model);
        $result = $this->Login_model->validate();
        if(!$result){
            $msg = '<div class="alert alert-danger" role="alert">Invalid username and/or password.</div><br />';
            $this->index($msg);
        }else{
            $msg = "welcome home";
            $this->index($msg);  
        }
        
    }
    */
    public function index(){
        $this->load->view('back_end/login_view');
    }

    public function login(){
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
        $this->form_validation->set_rules('password', 'Password', 'trim|required');

        #Kiá»ƒm tra Ä‘iá»�u kiá»‡n validate
        if($this->form_validation->run() == TRUE){
                $a_UserInfo['email'] = $this->input->post('inputEmail');
                $a_UserInfo['password'] = md5($this->input->post('inputPassword'));
                $a_UserChecking = $this->login_model->a_fCheckUser( $a_UserInfo );
                if($a_UserChecking){
                        $this->session->set_userdata('user', $a_UserChecking);
                        redirect(base_url('admin/success'));
                }else{
                        $this->b_Check = false;
                }
        }
        $a_Data['b_Check']= $this->b_Check;
        $this->load->view('back_end/login_view', $a_Data);

    }

    public function logout(){
            $this->session->unset_userdata('user');	// Unset session of user
            redirect(base_url('admin'));
    }

    public function success(){
            $a_UserInfo['email'] = $this->session->userdata('user');
            $this->load->view('back_end/login_success', $a_UserInfo);
    }
}

?>
